using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpaceWolf.GameLogic
{
    public class Stars
    {
        Vector3[] stars=new Vector3[20];
        Ship ship;
        Random rnd = new Random();

        public Stars(Ship ship)
        {
            this.ship = ship;

            int i;
            int nstars;

            //nstars = witchspace ? 3 : 12;
            nstars = 12;

            for (i = 0; i < nstars; i++)
            {
                stars[i].X = rnd.Next(600)-400;
                stars[i].Y = rnd.Next(400)-300;
                stars[i].Z = rnd.Next(255);

            }
            //warp_stars = 0;
        }

        public void front_starfield()
        {
            int i;
            float Q;
            float delta;
            float alpha = 0;
            float beta = 0;
            float xx, yy, zz;
            float sx;
            float sy;
            int nstars;

            nstars = 12;//witchspace ? 3 : 12;

            delta = ship.Speed;//warp_stars ? 50 : flight_speed;
            //alpha = ship.flight_roll;
            //beta = ship.flight_climb*100;
            //alpha *= 256.0f;
            delta /= 12.0f;

            for (i = 0; i < nstars; i++)
            {
                /* Plot the stars in their current locations... */

                sy = stars[i].Y;
                sx = stars[i].X;
                zz = stars[i].Z;

                sx += 400;
                sy += 300;

                if (
                    (sx >= 10) && (sx <= 800) &&
                    (sy >= 10) && (sy <= 600))
                {

                    Color color = (rnd.Next(2) == 1) ? new Color(167, 87, 167, 255) : new Color(223, 215, 207, 255);

                    SpaceWolf.BaseGame.DrawPixel(new Point((int)sx, (int)sy), color);
                //SpaceWolf.Game.BaseGame.DrawLine(new Point((int)sx, (int)sy),new Point((int)sx+1, (int)sy+1), Color.White);

                    if (zz < 192)
                        SpaceWolf.BaseGame.DrawPixel(new Point((int)sx + 1, (int)sy), color);

                    if (zz < 144)
                    {
                        SpaceWolf.BaseGame.DrawPixel(new Point((int)sx, (int)sy + 1), color);
                        SpaceWolf.BaseGame.DrawPixel(new Point((int)sx + 1, (int)sy + 1), color);
                        //        SpaceWolf.Game.BaseGame.DrawPixel(new Point((int)sx, (int)sy), Color.White);
                        //////        gfx_plot_pixel(sx, sy + 1, GFX_COL_WHITE);
                        //////        gfx_plot_pixel(sx + 1, sy + 1, GFX_COL_WHITE);
                    }
                }

                /* Move the stars to their new locations...*/

                Q = delta / stars[i].Z;

                stars[i].Z -= delta;
                yy = stars[i].Y + (stars[i].Y * Q);
                xx = stars[i].X + (stars[i].X * Q);
                zz = stars[i].Z;

                //yy = yy + (xx * alpha);
                xx = xx - (yy * alpha);

                ///*
                        //tx = yy * beta;
                        //xx = xx + (tx * tx * 2);
                //*/
                yy = yy + beta;

                stars[i].Y = yy;
                stars[i].X = xx;

                //if (warp_stars)
                //SpaceWolf.Game.BaseGame.DrawLine(new Point((int)sx, (int)sy), new Point((int)(xx + 128) * 2, (int)(yy + 96) * 2));

                sx = xx;
                sy = yy;

                if ((sx > 400) || (sx < -400) ||
                    (sy > 300) || (sy < -300) || (zz < 0))
                {
                    stars[i].X = rnd.Next(800) - 400;
                    stars[i].Y = rnd.Next(600) - 300;
                    stars[i].Z = rnd.Next(255);
                    continue;
                }
            }

            //warp_stars = false;
        }
    }
}
